package com.xqyq.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xqyq.model.Report;


@Repository
public class ReportDao {
	@Autowired
	private SessionFactory sessionFactory;
	
	
	public boolean addReport(Report report){
		Session session = sessionFactory.getCurrentSession();
		Query query= session.createQuery("from Report where report_name=?");
		query.setString(0, report.getReport_name());
		if(query.uniqueResult()==null){
			session.persist(report);
			return true;
		}
		else {
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Report> getAllReport(){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Report order by report_id desc");
		return query.list();
	}
	
	public Report getReport(int report_id){
		Session session = sessionFactory.getCurrentSession();
		return (Report) session.get(Report.class, report_id);
	}
	
	public void updateReport(Report report){
		Session session = sessionFactory.getCurrentSession();
		session.update(report);
		session.update(report.getReport_content());
	}
	
	public void deleteReport(int report_id){
		Session session = sessionFactory.getCurrentSession();
		session.delete(session.load(Report.class, report_id));
	}
	
	@SuppressWarnings("unchecked")
	public List<Report> getReports(int limit){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Report order by report_id desc");
		query.setMaxResults(limit);
		return query.list();
	}
}
